package com.geek.novel.dialog

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.animation.DecelerateInterpolator
import android.view.animation.Transformation
import android.widget.RelativeLayout
import com.geek.novel.R
import kotlin.math.abs


/**
 * 自定义的弹框
 */
abstract class CustomDialog(val context: Context): View.OnClickListener {
	
	private lateinit var dialogViews: List<View>
    
	lateinit var parent: RelativeLayout
	
    private var showing = false
	
	open fun onCreateView(){
		this.init()
		this.initDialog()
		this.bindEvent()
	}
	
    private fun init(){
		this.parent = this.initParent()
		
		//初始化容器控件
		this.dialogViews = initDialogViews(this.parent)
		this.initDialogViewsEvent(this.dialogViews)
	
		this.context as Activity
		val container = this.context.findViewById<ViewGroup>(android.R.id.content)
		container.addView(parent)
    }
	
	private fun initParent(): RelativeLayout{
		val parent = RelativeLayout(context)
		val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
		parent.layoutParams = params
		parent.visibility = View.GONE
		parent.background = this.dialogBackgroundDrawable()
		parent.setOnClickListener{
			this.close()
		}
		return parent
	}
	
	/**
	 * 给子容器添加点击事件，这样才不会点击就关闭弹框
	 */
	private fun initDialogViewsEvent(dialogViews: List<View>){
		for (view in dialogViews) {
			if(!view.hasOnClickListeners()){
				view.setOnClickListener{
				}
			}
		}
	}
	
	/**初始化容器控件*/
	abstract fun initDialogViews(parent: ViewGroup): List<View>

    /**弹框的布局宽度*/
    open fun dialogLayoutWidth(): Int{
        return ViewGroup.LayoutParams.MATCH_PARENT
    }
    /**弹框的布局高度*/
    open fun dialogLayoutHeight(): Int{
        return ViewGroup.LayoutParams.MATCH_PARENT
    }
    /**弹框的背景*/
    open fun dialogBackgroundDrawable(): Drawable?{
        return ColorDrawable()
    }

    /**初始化弹框*/
    abstract fun initDialog()

    /**绑定事件*/
    open fun bindEvent(){
    }
	
	open fun openDialogAnim(animList: List<Int>){
		if(this.showing){
			return
		}
		this.showing = true
		this.openDialog()
		for (i in animList.indices) {
			val anim = AnimationUtils.loadAnimation(context, animList[i])
			dialogViews[i].startAnimation(anim)
		}
	}
	
	open fun closeDialogAnim(animList: List<Int>){
		if(!this.showing){
			return
		}
		this.showing = false
		for (i in animList.indices) {
			val anim = AnimationUtils.loadAnimation(context, animList[i])
			dialogViews[i].startAnimation(anim)
			if(i == 0){
				anim.setAnimationListener(object: AnimListener(){
					override fun onAnimationEnd(animation: Animation?) {
						dismiss()
					}
				})
			}
		}
	}
	
	abstract class AnimListener: Animation.AnimationListener{
		override fun onAnimationStart(animation: Animation?) {
		}
		override fun onAnimationEnd(animation: Animation?) {
		}
		override fun onAnimationRepeat(animation: Animation?) {
		}
	}

    fun isShowing(): Boolean{
        return showing
    }

	open fun openDialog(){
		parent.visibility = View.VISIBLE
		this.showing = true
	}
	
	open fun dismiss() {
		parent.visibility = View.GONE
		this.showing = false
	}

	abstract fun open()

	abstract fun close()

    open fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}

	/**销毁控件*/
	open fun onDestroy() {
	}
	
	override fun onClick(v: View) {
		this.clickEvent(v.id, v)
	}
	
	/**绑定点击事件的回调*/
	protected abstract fun clickEvent(id: Int, v: View)
}